<ol class="breadcrumb">
    <li><a href="#">一元中奖</a></li>
    <li>中奖管理</li>
</ol>
<div class="panel panel-default">
    <div class="panel-heading">
        <span class="icon-gift icon-large"></span>
        <span class="panel-title">中奖列表</span>
    </div>
    <% if(session.permissionMap[3008]){ %>
    <div class="panel-body">
        <div class="col-xs-2">
            <select id="dispatchid" class="form-control">
                <% dispatchs.forEach(function(dispatch){ %>
                <option value="<%= dispatch.id %>"><%= dispatch.name %></option>
                <% }); %>
            </select>
        </div>
        <div class="col-xs-4">
            <input type="text" id="dispatchNum" class="form-control" id="dispatchNum" placeholder="请输入订单编号">
        </div>
        <div class="form-group">
            <a href="#" onclick="dispatch()">
                <button type="button" class="btn btn-success">发货</button>
            </a>&nbsp;&nbsp;
        </div>
    </div>
    <% } %>
    <div class="table-responsive">
        <table class="table table-hover table-striped" id="lotteryTable">
            <thead>
            <tr>
                <th>√</th>
                <th>中奖号码</th>
                <th>商品名称</th>
                <th>收货信息</th>
                <th>快递公司</th>
                <th>物流单号</th>
                <th>开奖时间</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <td colspan="11">
                    <nav class="twbsPagination"></nav>
                </td>
            </tr>
            </tfoot>
            <tbody>
            <% data.rows.forEach(function(orders, index){ %>
            <tr>
                <th><input name="lottery_id" type="checkbox" value="<%= orders.id %>"></th>
                <td><%= orders.lottery %></td>
                <td><%= orders.title %></td>
                <td><%= orders.address %></td>
                <td><%= orders.name %></td>
                <td><%= orders.dispatchNum %></td>
                <td><%= orders.createtime %></td>
            </tr>
            <% }) %>

            </tbody>
        </table>
    </div>
</div>
<style>
    .thumb {
        width: 30%;
        /*background-image: url("http://123.57.72.99:3000/images/goods/5881b28b2f6e8730d409a251e83c9a0f.jpg");*/
        background-repeat: no-repeat;
        background-size: cover;
        height: 80px;
        width: 80px;
        margin: 3px;
    }
</style>
<script>
    /*
     $('#openModal').click(function(){
     $('#myModal').modal('toggle');
     });*/
    init(<%= data.total%>);//初始化分页
    function init(total) {
        //分页功能
        $('.twbsPagination').twbsPagination({
            totalPages: parseInt((Number(total) + 10 - 1) / 10, 10),
            visiblePages: 10,
            onPageClick: function (event, page) {
                reloadpage(page, false);
            }
        });
    }
    function reloadpage(page, isinit) {
        $.get("/lottery/list",
                {page: page, dispatch: 'false'},
                function (result) {
                    if (isinit) {
                        init(result.total);
                    }

                    if (result.rows.length < 1) {
                        $('#rowcontent').html('无数据...');
                        return;
                    }
                    var html = '';
                    result.rows.forEach(function (orders, index) {
                        var status = '已发货';
                        if (orders.dispatchStatus == 'no') {
                            status = '未发货';
                        }
                        html += '<tr><td><input name="lottery_id" type="checkbox" value="' + orders.id + '"></td>' +
                                '<td>' + orders.lottery + '</td>' +
                                '<td>' + orders.title + '</td>' +
                                '<td>' + orders.address + '</td>' +
                                '<td>' + orders.name + '</td>' +
                                '<td>' + orders.dispatchNum + '</td>' +
                                '<td>' + orders.createtime + '</td>' +
                                '</tr>';
                    });
                    $('#lotteryTable tbody').html(html);
                });
    }

    //发货
    function dispatch() {
        var id = selectid();
        if (!id) {
            return alert('请选择一条记录!');
        }
//        return alert(id);
        $.post('/lottery/update', {
            id: id,
            dispatchStatus: 'yes',
            dispatchid: $('#dispatchid').val(),
            dispatchNum: $('#dispatchNum').val()
        }, function (result) {
            if (result.msg) {
                location.reload(true);
            } else {
                alert('失败！');
            }
        });
    }

    function selectid() {
        var adIds = "";
        $("input:checkbox[name='lottery_id']:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();

            }
        });
//        if(!adIds){
//            return alert('请选择一条记录!');
//        }
        return adIds;
    }
</script>